package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.cargo.*;
import com.itheima.doamin.cargo.*;
import com.itheima.service.cargo.ExportService;
import com.itheima.vo.ExportProductResult;
import com.itheima.vo.ExportResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class ExportServiceImpl implements ExportService {
    @Autowired
    ExportDao exportDao;
    @Autowired
    ContractDao contractDao;
    @Autowired
    ContractProductDao contractProductDao;
    @Autowired
    ExportProductDao exportProductDao;
    @Autowired
    ExtCproductDao extCproductDao;
    @Autowired
    ExtEproductDao extEproductDao;

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        //设置id和报单初始化状态
        export.setId(UtilFuns.getUUID());
        export.setState(0);
        export.setCreateTime(new Date());
        //获得每一个合同
        String[] contractIds= export.getContractIds().split(",");
        //遍历每一个id
        String contractNos="";
        for (String contractid:contractIds){
            //获得每一个合同进行更新
            Contract contract = contractDao.selectByPrimaryKey(contractid);
            //更新合同的状态为已报运
            contract.setState(2);
            //生成确认书
            contractNos = contract.getContractNo()+" ";
            contractDao.updateByPrimaryKeySelective(contract);
        }
        export.setCustomerContract(contractNos);


        //货物
        //创建货物的查询条件
        ContractProductExample contractProductExample=new ContractProductExample();
        contractProductExample.createCriteria().andContractIdIn(Arrays.asList(contractIds));
        //开始进行查询
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(contractProductExample);

        //创建map在附件是判断附件保单属于哪个货物报单
        Map<String,String> exportProductMap=new HashMap<>();
        //遍历货物
        for (ContractProduct contractProduct:contractProducts){
            //创建货物保单对象
            ExportProduct exportProduct=new ExportProduct();
            BeanUtils.copyProperties(contractProduct,exportProduct);
            String exportProductId=UtilFuns.getUUID();
            exportProduct.setId(exportProductId);
            //报单货物和保单产生关联
            exportProduct.setExportId(export.getId());
            //将数据保存至数据库
            exportProductDao.insertSelective(exportProduct);
            //
            exportProductMap.put(contractProduct.getId(),exportProductId);
        }
        //获得附件
        ExtCproductExample extCproductExample=new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdIn(Arrays.asList(contractIds));
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        for (ExtCproduct extCproduct:extCproducts){
            //创建附件出口报单对象
            ExtEproduct extEproduct=new ExtEproduct();
            //copy属性
            BeanUtils.copyProperties(extCproduct,extEproduct);
            //设置id
            extEproduct.setId(UtilFuns.getUUID());
            //报单与报单附件建立关系
            extEproduct.setExportId(export.getId());
            //判断报单附件属于哪个货物报单,并建立关系
            extEproduct.setExportProductId(exportProductMap.get(extCproduct.getContractProductId()));
            extEproductDao.insertSelective(extEproduct);
        }
        //设置货物数和附件数
        export.setExtNum(extCproducts.size());
        export.setProNum(contractProducts.size());
        //将报单插入数据库
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);
        List<ExportProduct> exportProducts = export.getExportProducts();
        for (ExportProduct exportProduct:exportProducts) {
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }

    @Override
    public void delete(String id) {

    }

    //查询已经报运的合同
    @Override
    public PageInfo findAll(ContractExample example, int page, int size) {
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page,size);
        return new PageInfo(contractDao.selectByExample(example));
    }

    /**
     * 进行报运后海关返回的结果
     *
     * @param exportResult
     */
    @Override
    public void updateE(ExportResult exportResult) {
        //获取到报运后的结果
        String exportId = exportResult.getExportId();
        Export export=new Export();
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        export.setId(exportId);
        exportDao.updateByPrimaryKeySelective(export);

        //对货物进行更新
        Set<ExportProductResult> exportProductResults = exportResult.getProducts();
        //对set集合进行遍历
        for (ExportProductResult exportProductResult:exportProductResults){
            //
            ExportProduct exportProduct=new ExportProduct();
            exportProduct.setId(exportProductResult.getExportProductId());
            exportProduct.setTax(exportProductResult.getTax());
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }
    }
    //所有出口数据保单
    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page,size);
        return new PageInfo(exportDao.selectByExample(example));
    }
}
